/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.system.auth.cachemanager.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.modules.system.auth.resourcemanager.model.PlatResource;
import cn.com.ksplatform.modules.system.auth.resourcemanager.service.PlatResourceService;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

@Component("system.modules.system.cache.ResourceCacheService")
public class ResourceCacheService {
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	@Autowired
	PlatResourceService platResourceService;
	
	Cache resourceCache = null;
	
	public Cache getCache(){
		if(resourceCache == null){
			resourceCache = cacheManager.getCache("ResourceCache");
		}
		return resourceCache;
	}
	
	
	
	public void init(){
		List<PlatResource> resources = platResourceService.getAllRess();
		for(PlatResource resource : resources){
			Element em = new Element(resource.getResourceid(), resource);
			getCache().put(em);
		}
		Log.info("初始化系统资源缓存数量:"+resources.size());
	}
	
}
